//
//  ESTFollowUpRecordTableViewCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/24.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFollowUpRecordTableViewCell.h"
#define FollowUpRecordTextWordsMax    15
#define FollowUpRecordTextValueMax    99
@implementation ESTFollowUpRecordCellModel

@end

@interface ESTFollowUpRecordTableViewCell ()<UITextFieldDelegate>
/**
 *  左边标签
 */
@property(nonatomic, strong) TYZLabel *leftLabel;
/**
 *  右边View
 */
@property(nonatomic, strong) TYZView *rightView;
/**
 *  右边输入框
 */
@property(nonatomic, strong) TYZTextField *textField;
/**
 *  右边的输入View
 */
@property(nonatomic, strong) TYZTextView *textView;
@end

@implementation ESTFollowUpRecordTableViewCell
/**
 快速创建Cell

 @param tableView tableView
 @return Cell
 */
+ (instancetype)creatFollowUpRecordTableViewCell:(UITableView *)tableView {
    ESTFollowUpRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTFollowUpRecordTableViewCell"];
    if (cell == nil) {
        cell = [[ESTFollowUpRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTFollowUpRecordTableViewCell"];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 左边标题
        TYZLabel *leftLabel = [[TYZLabel alloc] init];
        [self.contentView addSubview:leftLabel];
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(20);
        }];
        leftLabel.textColor = TYZLightColor;
        leftLabel.font = FollowUpRecordHeaderViewFont;
        self.leftLabel = leftLabel;
        
        // 右边View
        TYZView *rightView = [[TYZView alloc] init];
        [self.contentView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(120);
            make.right.equalTo(self.contentView).offset(-10);
        }];
        self.rightView = rightView;
        
        TYZView *line = [[TYZView alloc] init];
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(20);
            make.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(1);
            make.right.equalTo(self.contentView).offset(-10);
        }];
        line.backgroundColor = ESTBackgroundColor;
    }
    return self;
}
- (void)setModel:(ESTFollowUpRecordCellModel *)model {
    _model = model;
    [self.rightView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    switch (model.cellType) {
        case FollowUpTableViewCellType_TextField:
            if (model.maxWords == 0) {
                model.maxWords = FollowUpRecordTextWordsMax;
            }
            [self addTextFieldWithEnable:YES];
            break;
        case FollowUpTableViewCellType_Number:
            if (model.maxValue == 0) {
                model.maxValue = FollowUpRecordTextValueMax;
            }
            [self addNumber];
            break;
        case FollowUpTableViewCellType_Picker:
            [self addTextFieldWithEnable:NO];
            [self addRightArrow];
            break;
        default:
            break;
    }
    self.leftLabel.text = model.leftTitle;
    self.textField.placeholder = model.placeholder;
    self.textField.text = model.rightText;
    self.textField.tag = model.textFieldTag;
    self.textField.enabled = !model.isNotEnable;
}
#pragma mark - 设置UI
- (void)addTextFieldWithEnable:(BOOL)enable {
    TYZTextField *textField = [[TYZTextField alloc] init];
    [self.rightView addSubview:textField];
    textField.borderStyle = UITextBorderStyleNone;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.rightView);
    }];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.returnKeyType = self.model.returnKeyType == UIReturnKeyNext ? UIReturnKeyNext : UIReturnKeyDone;
    textField.keyboardType = self.model.keyboardType;
    textField.font = FollowUpRecordHeaderViewFont;
    textField.textColor = TYZBlackColor;
    textField.delegate = self;
    textField.enabled = enable;
    self.textField = textField;
    
    WEAKSELF
    [[NSNotificationCenter defaultCenter] addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
        TYZTextField *textField = (TYZTextField *)note.object;
        if (textField.tag == self.model.textFieldTag) {
            if (weakSelf.didComplementBlock) {
                weakSelf.didComplementBlock(textField.text);
            }
        }
    }];
}
- (void)addRightArrow {
    TYZImageView *iconView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right_999"]];
    [self.rightView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rightView);
        make.right.equalTo(self.rightView);
        make.width.height.mas_equalTo(20);
    }];
    TYZButton *coverButton = [TYZButton buttonWithType:UIButtonTypeCustom];
    [self.rightView addSubview:coverButton];
    [coverButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.rightView);
    }];
    [coverButton addTarget:self action:@selector(didClickCoverButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didClickCoverButton {
    if (self.didClickCoverBlock) {
        self.didClickCoverBlock(self.model.leftTitle);
    }
}
- (void)addNumber {
    UIFont *font = [UIFont systemFontOfSize:CoreInfo.fontSize * 1.2];
    CGFloat width = 20;
    TYZButton *leftButton = [[TYZButton alloc] init];
    [self.rightView addSubview:leftButton];
    [leftButton setTitle:@"-" forState:UIControlStateNormal];
    [leftButton setTitleColor:TYZLightColor forState:UIControlStateNormal];
    leftButton.layer.borderColor = TYZLightColor.CGColor;
    leftButton.layer.borderWidth = 1;
    leftButton.titleLabel.font = font;
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.leftLabel);
        make.left.equalTo(self.rightView);
        make.width.height.mas_equalTo(width);
    }];
    TYZTextField *textField = [[TYZTextField alloc] init];
    [self.rightView addSubview:textField];
    textField.font = font;
    textField.textColor = TYZBlackColor;
    textField.textAlignment = NSTextAlignmentCenter;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftButton);
        make.left.equalTo(leftButton.mas_right);
        make.width.mas_equalTo(width * 2);
        make.height.mas_equalTo(width);
    }];
    textField.delegate = self;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.returnKeyType = UIReturnKeyDone;
    textField.borderStyle = UITextBorderStyleNone;
    textField.tag = TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON;
    self.textField = textField;
    TYZButton *rightButton = [[TYZButton alloc] init];
    [self.rightView addSubview:rightButton];
    [rightButton setTitle:@"+" forState:UIControlStateNormal];
    [rightButton setTitleColor:TYZLightColor forState:UIControlStateNormal];
    rightButton.layer.borderColor = TYZLightColor.CGColor;
    rightButton.layer.borderWidth = 1;
    rightButton.titleLabel.font = font;
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftButton);
        make.left.equalTo(textField.mas_right);
        make.width.height.mas_equalTo(width);
    }];
    [leftButton addTarget:self action:@selector(didClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - actions
- (void)didClickLeftButton {
    NSInteger value = [self.textField.text integerValue];
    value -= 1;
    if (value < 1) {
        value = 1;
    }
    self.textField.text = [NSString stringWithFormat:@"%ld", (long)value];
    if (self.didComplementBlock) {
        self.didComplementBlock(self.textField.text);
    }
}
- (void)didClickRightButton {
    NSInteger value = [self.textField.text integerValue];
    value += 1;
    if (value > self.model.maxValue) {
        value = self.model.maxValue;
    }
    self.textField.text = [NSString stringWithFormat:@"%ld", (long)value];
    if (self.didComplementBlock) {
        self.didComplementBlock(self.textField.text);
    }
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if (textField.text.length + string.length > self.model.maxWords) {
        return NO;
    }
    if (textField.tag == TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON) {
        if ([string isValidateRegex:Regex_Number]) {
            if ([textField.text integerValue] > 99) {
                return NO;
            }
            return YES;
        }
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (self.didComplementBlock) {
        self.didComplementBlock(textField.text);
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == TAG_SIXTH_ACTIVITY_FOLLOWUP_PERSON) {
        NSInteger integer = [textField.text integerValue];
        if (integer > self.model.maxValue) {
            integer = self.model.maxValue;
        }
        if (integer < 1) {
            integer = 1;
        }
        textField.text = [NSString stringWithFormat:@"%ld", (long)integer];
        return NO;
    }
    return YES;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
